package com.system.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result(){}

    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    protected static <T> Result<T> build(String message,ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<T>();
        if (message != null)
            result.setMessage(message);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(message);
        return result;
    }
    
    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

  
    public static <T> Result<T> build(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static<T> Result<Object> ok(){
        return Result.ok(null);
    }

    /**
     * 操作成功
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        Result<T> result = build(data);
        return build(data,ResultCodeEnum.SUCCESS);
    }

    /**
     * 操作成功
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> loginok(T data){
        Result<T> result = build(data);
        return build(data, ResultCodeEnum.LOGIN_SUCCESS);
    }
    
    public static<T> Result<Object> fail(){
        return Result.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }
    /**
     * 操作失败
     * @param
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(String message){
        return build(message, ResultCodeEnum.FAIL);
    }
    
    /**
     * 操作失败
     * @param
     * @param <T>
     * @return
     */
    public static<T> Result<T> loginfail(String message){
        return build(message, ResultCodeEnum.LOGIN_FAIL);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public boolean isOk() {
        if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }

		 /**
	     * token超时
	     * @param
	     * @param <T>
	     * @return
	     */
	    public static<T> Result<T> tokenOverTime(String message){
	        return build(message, ResultCodeEnum.TOKEN_OVERTIME);
	    }
}
